<?php
//set_time_limit(0);
//ignore_user_abort(1);
header('Location:http://okhati.org/');
// Is the device a mobile device with location sensor?
include('/home/content/o/k/h/okh1234/html/test/wp-blog-header.php');
global $wpdb;

$ismobile = false;
if($ismobile == true)
$sensor = 'true';
else
$sensor = 'false';

$iid = $_GET['id'];
$max = $_GET['max'];
// Query database
$query = "SELECT * FROM institution_table WHERE (institution_id >= $iid AND institution_id <= $max)";//institution_id=$iid

$res = $wpdb->get_results($query,ARRAY_A);
//print_r($res);
if($res){
	foreach($res as $row){
		// Pull required variables
		$id = $row['institution_id'];
		$name = $row['name'];
		$dist = $row['district_name'];
		$city = $row['city_name'];
		$ward = $row['ward_no'];
		
		$myPlace = array();
		$myPlace['institution_id'] = $id;
		$myPlace['name'] = $name;
		$myPlace['description'] = '';
		$myPlace['latitude'] = 27.7167;
		$myPlace['longitude'] = 85.3667;
		//print_r($row);
		// Create url geocoding api
		// Google normal
		$gComplete = 'http://maps.google.com/maps/api/geocode/json?address='.urlencode($name).'&components=country:NP&sensor='.$sensor;
		// Geonames city search when place not found in Google
		$gCity = 'http://api.geonames.org/searchJSON?q='.urlencode($city).'&country=NP&fuzzy=0.8&username=okhatitech';
		$gDist = 'http://api.geonames.org/searchJSON?q='.urlencode($dist).'&country=NP&fuzzy=0.8&username=okhatitech';
		// Call Google geocoding api first
		$gOut = json_decode(file_get_contents($gComplete),true);
		//print_r($gOut);		
		
		//echo "<br />Next:<br />";
		// Parse Json
		if($gOut['status'] == "OK"){ // Query went OK
			$myPlace['description']=$gOut['results'][0]['formatted_address'];
			$myPlace['latitude']=$gOut['results'][0]['geometry']['location']['lat'];
			$myPlace['longitude']=$gOut['results'][0]['geometry']['location']['lng'];
		}
		if($gOut['status'] != "OK" || $myPlace['description'] == "Nepal"){ // Address was not found
			$myPlace['description']=$city.', Ward #'.$ward.', '.$dist;
			$myPlace['APPROX'] = 1;
			// Call Geonames api next
			// Search for City
			$gOut = json_decode(file_get_contents($gCity),true);
			if($gOut['totalResultsCount'] > 0){ // Some result was found
				$myPlace['latitude']=$gOut['geonames'][0]['lat'];
				$myPlace['longitude']=$gOut['geonames'][0]['lng'];
			}
			else{ // Search for District
				$gOut = json_decode(file_get_contents($gDist),true);
				if($gOut['totalResultsCount'] > 0){ // Some result was found
					$myPlace['latitude']=$gOut['geonames'][0]['lat'];
					$myPlace['longitude']=$gOut['geonames'][0]['lng'];
				}			
			}
		}
		//print_r($gOut);
		
		//echo "<br />Next:<br />";
		//print_r($myPlace);
		/*Insert into database*/
		$wpdb->insert(
			'om_points',
			$myPlace,
			array(
				'%d',
				'%s',
				'%s',
				'%f',
				'%f',
				'%d'
			)
		);
		
	}

}
echo "Done!";
 //$wpdb->print_error(); 
?>